package com.sally.android;

import java.io.Console;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.Toast;
import android.util.*;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

//import com.google.android.maps.MapView;


public class MapLocationActivity extends MapActivity implements LocationListener
{
	protected MapView mapview;
	private MapController mapcontroller;
	private GeoPoint point;

	  
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplocation);
        mapview = (MapView) findViewById(R.id.mapview);
        mapcontroller = mapview.getController();

        LocationManager locationManager = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLocationChanged(Location location) {
		double lat = 0.0;
		double lng = 0.0 ;
		if (location != null) {
			lat = location.getLatitude();
			lng = location.getLongitude();
			lat = lat * 1e6;
			lng = lng * 1e6;
		}
		point = new GeoPoint((int)lat, (int)lng);
		mapcontroller.animateTo(point);
		mapcontroller.setZoom(16);

		//---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapview.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        
 
        mapview.invalidate();
        
        

	}

	public void onProviderDisabled(String arg0) {
	}

	public void onProviderEnabled(String arg0) {
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		//myLocationOverlay.enableMyLocation();
		//myLocationOverlay.enableCompass();
	}
	@Override
	protected void onPause() {
		super.onResume();
		//myLocationOverlay.disableMyLocation();
		//myLocationOverlay.disableCompass();
	}
	
	class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
 
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(point, screenPts);
 
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.marker);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 
}